﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using System.IO;

using System.Reflection; 







namespace KinectProject
{
    /// <summary>
    /// Interaction logic for Page3.xaml
    /// </summary>

    public partial class recordsPage : Page
    {

        VRGETTableAdapters.RecordsAdapter rAdapter;
        VRGET.RecordsDataTable rRecords;


        public recordsPage()
        {
            InitializeComponent();
            rAdapter = new VRGETTableAdapters.RecordsAdapter();
        }

        //   dataGrid1.DataSource = controller.dSet.Tables["Student"].DefaultView;
        //            dbController db = new dbController(this);


        //private void textBox1_KeyDown(object sender, KeyEventArgs e)
        //{
        //    //if (e.Key == Key.Enter)
        //    //{}

        //}

        private void button1_Click(object sender, RoutedEventArgs e)
        {



            if (textBoxFName.Text == "")
            {
                rRecords = rAdapter.GetRecords();

            }
            else if (textBoxLName.Text == "")
            {
                MessageBox.Show("Please Enter Last Name");
            
            }else
            {

                rRecords = rAdapter.GetDataByFL(textBoxFName.Text, textBoxLName.Text);
                //rRecords = rAdapter.GetRecordsByFirst(textBoxFName.Text);
            }

            dataGrid1.ItemsSource = rRecords;

            //foreach (VERGTDatabase.RecordsDataTableRow productRow in products)
            //  textBox1.Text = productRow.TerritoryDescription;

            //if (e.Key == Key.Enter)
            //{
            //    dbController controller = new dbController(this);
            //    controller.getRecords(textBox1.Text);
            // }
        }

        private void export_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                // Create the CSV file to which grid data will be exported.
                var resultPath = log.logFolder + log.lName + "_" + log.fName + "_" + DateTime.Now.ToString();
                
                StreamWriter sw = new StreamWriter(resultPath, false);

                int iColCount = rRecords.Columns.Count;

                for (int i = 0; i < iColCount; i++)
                {
                    sw.Write(rRecords.Columns[i]);
                    if (i < iColCount - 1)
                    {
                        sw.Write(",");
                    }
                }
                sw.Write(sw.NewLine);
       

                foreach (VRGET.RecordsRow dr in rRecords.Rows)
                {
                    for (int i = 0; i < iColCount - 2; i++)
                    {
                        if (!Convert.IsDBNull(dr[i]))
                        {
                            sw.Write(dr[i].ToString());
                        }

                        if (i < iColCount - 1)
                        {
                            sw.Write(",");
                        }

                    }

                    sw.Write(sw.NewLine);

                }
                sw.Close();
                MessageBox.Show("Table written to c:/testlog.csv");
            
            
            }
            catch (Exception ex)
            {
               MessageBox.Show("ERROR");
            }









        }//END click

 



    
    
    
    
    
    }
}









